package.path = package.path ..';../?.lua'
local publish = require('publish/publish')
_G.EXPORT_ASSERT_TO_GLOBALS = true
require('lib/luaunit')
_G.testPublish = {}


function testPublish:setUp()
    self.msg_center = publish.provider:New()
    -- self.publisher = publish.publisher:New()
    -- self.publisher:addMsgCenter(self.msg_center)
end

function testPublish:test_scribeAndPublish_base()
    local message_center = publish.provider:New()
    local publisher = publish.publisher:New()
    publisher:addMsgCenter(message_center)


    local sendTime = os.time()
    local msg = { a ="1";b = 2}
    local msg1 = publish.Msg:New(sendTime,publish.msgType.Normal,msg)
    local msg2 = publish.Msg:New(222,publish.msgType.UI)
    local msg3 = publish.Msg:New(0,publish.msgType.Skill)

    local jim = publish.subscriber:New('jim',message_center)
    jim:subscribe(publish.msgType.Skill)
    jim.getMsg = function(aSubscriber,msg)
        jim.bGetMsg = true
    end

    publisher:publish(msg1)
    publisher:publish(msg2)
    publisher:publish(msg3)
    local tStart = os.time()
    local tInit = os.time()
    while tInit - tStart < 1 do
        local tNow = os.time()
        if tNow - tInit > 0.1 then
            tInit = tNow
            message_center:update(tNow)
        end
    end
    assertTrue(jim.bGetMsg)
end

function testPublish:test_scribeAndPublish_msgRank()
    local message_center = publish.provider:New()
    local publisher = publish.publisher:New()
    publisher:addMsgCenter(message_center)


    local msg = { a ="1";b = 2}
    local msg1 = publish.Msg:New(333,publish.msgType.Skill,msg)
    local msg2 = publish.Msg:New(222,publish.msgType.Skill)
    local msg3 = publish.Msg:New(0,publish.msgType.Skill)

    local jim = publish.subscriber:New('jim',message_center)
    jim:subscribe(publish.msgType.Skill)
    jim.tbMsg = {}
    jim.getMsg = function(aSubscriber,msg)
        table.insert(aSubscriber.tbMsg,msg)
    end

    publisher:publish(msg1)
    publisher:publish(msg2)
    publisher:publish(msg3)
    local tStart = os.time()
    local tInit = os.time()
    local timeInterval = 1
    for i=1 ,10000 do
        local tNow = tStart + i
        if tNow - tInit > 1 then
            tInit = tNow
            message_center:update(tNow, timeInterval)
        end
    end

    assertEquals(#jim.tbMsg, 3)
    assertTrue(jim.tbMsg[1] == msg3)
    assertTrue(jim.tbMsg[2] == msg2)
    assertTrue(jim.tbMsg[3] == msg1)
end

function testPublish:test_scribeAndPublish_msgRank_sameMsgInSmallestInterval()
    local message_center = publish.provider:New()
    local publisher = publish.publisher:New()
    publisher:addMsgCenter(message_center)

    local msg1 = publish.Msg:New(0,publish.msgType.Skill)
    local msg2 = publish.Msg:New(0.19,publish.msgType.Skill)
    local msg3 = publish.Msg:New(0.10,publish.msgType.Skill)
    local msg4 = publish.Msg:New(3,publish.msgType.Skill)

    local jim = publish.subscriber:New('jim',message_center)
    jim:subscribe(publish.msgType.Skill)
    jim.tbMsg = {}
    jim.getMsg = function(aSubscriber,msg)
        table.insert(aSubscriber.tbMsg,msg)
    end

    publisher:publish(msg1)
    publisher:publish(msg2)
    publisher:publish(msg3)
    publisher:publish(msg4)
    local tStart = os.time()
    local tInit = os.time()
    local timeInterval = 1
    for i=1 ,10000 do
        local tNow = tStart + i
        if tNow - tInit > 1 then
            tInit = tNow
            message_center:update(tNow, timeInterval)
        end
    end

    assertTrue(#jim.tbMsg, 2)
    assertTrue(jim.tbMsg[1] == msg1)
    assertTrue(jim.tbMsg[1] == msg2)
    assertTrue(jim.tbMsg[1] == msg3) 
    assertTrue(jim.tbMsg[2] == msg4) 
end


local lu = LuaUnit.new()
lu:setOutputType("tap")
os.exit( lu:runSuite() )
